package ee.maoinvest.frontend.controller.command.electricitymetercommands;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import ee.maoinvest.backend.model.data.ElectricityMeter;
import ee.maoinvest.backend.model.data.ElectricityMeterAndItsElectricityContractPeriod;
import ee.maoinvest.backend.services.ElectricityMeterService;
import ee.maoinvest.backend.services.EnterpriseRelationsWithElectricityMetersAndContractsService;
import ee.maoinvest.backend.services.implementations.ElectricityMeterServiceImpl;
import ee.maoinvest.backend.services.implementations.EnterpriseRelationsWithElectricityMetersAndContractsServiceImpl;
import ee.maoinvest.frontend.controller.command.Command;
import ee.maoinvest.frontend.controller.formstringdataconverters.electricitymeterforms.ReplaceOrDeleteSubstationMainElectricityMeterFormConverter;
import ee.maoinvest.frontend.controller.requestprocessors.ElectricityMeterFormsRequestProcessor;

public class GetAndFillReplaceOrDeleteSubstationMainElectricityMeterFormWithInitialDataCommand implements Command
{
	private int operationResult = 0;
	private final int OPERATION_SUCCESSFUL = 1;
	private final int OPERATION_UNSUCCESSFUL = 0;
	
	private final int ID_PARSE_ERROR_IDENTIFICATOR = -1;
	
	@Override
	public int executeCommand(HttpServletRequest request,
			HttpServletResponse response) 
	{
		ElectricityMeterFormsRequestProcessor electricityMeterFormsRequestProcessor = null;
		ReplaceOrDeleteSubstationMainElectricityMeterFormConverter replaceOrDeleteSubstationMainElectricityMeterFormConverter = null;
		ElectricityMeter substationMainElectricityMeter = null;
		ElectricityMeterService electricityMeterService = null;
		List<ElectricityMeter> listOfPotentialParentElectricityMeters = null;
		List<ElectricityMeter> listOfRelatedChildElectricityMeters = null;
		EnterpriseRelationsWithElectricityMetersAndContractsService enterpriseRelationsWithElectricityMetersAndContractsServiceImpl = null;
		ElectricityMeterAndItsElectricityContractPeriod electricityMeterAndItsElectricityContractPeriod = null;
		String enterpriseNameMeterIsHavingContractWith = null;
		String contractNameMadeOnMeter = null;
		electricityMeterFormsRequestProcessor = new ElectricityMeterFormsRequestProcessor();
		
		int substationMainElectricityMeterIDToReplace = electricityMeterFormsRequestProcessor.getSubstationMainElectricityMeterIDToReplaceOrDelete(request);
		
		if (substationMainElectricityMeterIDToReplace != ID_PARSE_ERROR_IDENTIFICATOR)
		{
			electricityMeterService = new ElectricityMeterServiceImpl();
			listOfPotentialParentElectricityMeters = new ArrayList<ElectricityMeter>();
			listOfRelatedChildElectricityMeters = new ArrayList<ElectricityMeter>();
			enterpriseRelationsWithElectricityMetersAndContractsServiceImpl = 
					new EnterpriseRelationsWithElectricityMetersAndContractsServiceImpl();
			
			substationMainElectricityMeter = electricityMeterService.getElectricityMeterByID(substationMainElectricityMeterIDToReplace);
			listOfPotentialParentElectricityMeters = electricityMeterService.getListOfPotentialParentElectricityMeters(
					substationMainElectricityMeterIDToReplace);
			listOfRelatedChildElectricityMeters = electricityMeterService.getListOfRelatedChildElectricityMeters(
					substationMainElectricityMeterIDToReplace);
			electricityMeterAndItsElectricityContractPeriod = 
					enterpriseRelationsWithElectricityMetersAndContractsServiceImpl.getElectricityMeterAndItsElectricityContractPeriodByElectricityMeterID(
							substationMainElectricityMeterIDToReplace);
			enterpriseNameMeterIsHavingContractWith = electricityMeterService.getEnterpriseNameMeterIsHavingContractWith(
					substationMainElectricityMeterIDToReplace);
			contractNameMadeOnMeter = electricityMeterService.getContractNameMadeOnMeter(
					substationMainElectricityMeterIDToReplace);
			
			if (substationMainElectricityMeter != null)
			{
				replaceOrDeleteSubstationMainElectricityMeterFormConverter = 
						new ReplaceOrDeleteSubstationMainElectricityMeterFormConverter();
				replaceOrDeleteSubstationMainElectricityMeterFormConverter.fillWithInitialData(
						substationMainElectricityMeter, listOfPotentialParentElectricityMeters, 
						listOfRelatedChildElectricityMeters,
						electricityMeterAndItsElectricityContractPeriod, enterpriseNameMeterIsHavingContractWith, 
						contractNameMadeOnMeter);
				
				request.setAttribute("replaceOrDeleteSubstationMainElectricityMeterFormConverter", 
						replaceOrDeleteSubstationMainElectricityMeterFormConverter);
				
				HttpSession session = request.getSession();
				session.setAttribute("substationMainElectricityMeter", substationMainElectricityMeter);
				session.setAttribute("listOfPotentialParentElectricityMeters", listOfPotentialParentElectricityMeters);
				session.setAttribute("listOfRelatedChildElectricityMeters", listOfRelatedChildElectricityMeters);
	
				operationResult = OPERATION_SUCCESSFUL;
			}
		}
		else
		{
			operationResult = OPERATION_UNSUCCESSFUL;
		}

		return operationResult;
	}
}